//
//  THJGuideVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/11.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJGuideVC.h"
#import "LBTabBarController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface THJGuideVC ()<UIScrollViewDelegate,ABPeoplePickerNavigationControllerDelegate>

@end

@implementation THJGuideVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setScrollview];
//     [self getPhoneContact];
   // self.view.backgroundColor = SingleColor(whiteColor);
    // Do any additional setup after loading the view from its nib.
}
-(void)setScrollview{
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(ScreenWidth*3, ScreenHeight);
    scrollView.backgroundColor = SingleColor(whiteColor);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    for (NSInteger i = 0; i<3; i++) {
        UIImageView * imgV = [[UIImageView alloc] init];
        imgV.frame = CGRectMake(i*(ScreenWidth), 0, ScreenWidth, ScreenHeight);
        NSString * imgName = [NSString stringWithFormat:@"timg%ld%@",i+1,@".jpg"];
        imgV.image = kGetImage(imgName);
        imgV.userInteractionEnabled = YES;
        [scrollView addSubview:imgV];
        if (2 == i) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
            btn.frame = CGRectMake((ScreenWidth-220)/2, ScreenHeight-70, 220, 45);
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 5;
            [btn setBackgroundImage:kGetImage(@"ljty") forState:UIControlStateNormal];
            //btn.backgroundColor = SingleColor(brownColor);
            [btn addTarget:self action:@selector(enterMainPage) forControlEvents:UIControlEventTouchUpInside];
            [imgV addSubview:btn];
        }
    }
   
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
-(void)enterMainPage{
    NSLog(@"进入主页");
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [THJTabBarController sharedTabBarC];
}
#pragma mark ---contact----
-(void)getPhoneContact{
    ABAddressBookRef addressBookref = ABAddressBookCreateWithOptions(NULL, NULL);
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookref, ^(bool granted, CFErrorRef error) {
            CFErrorRef *error1 = NULL;
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
            [self copyAddressBook:addressBook];
        });
    }else if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
        CFErrorRef *error1 = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
        [self copyAddressBook:addressBook];
    }else{
        //        UIAlertView * alert  = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有获取通讯录权限" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        //        alert.delegate = self;
        //        [alert show];
    }
}
- (void)copyAddressBook:(ABAddressBookRef)addressBook{
    //获取联系人个数
    CFIndex numberOfPeoples = ABAddressBookGetPersonCount(addressBook);
    CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSLog(@"有%ld个联系人", numberOfPeoples);
    //循环获取联系人
    NSMutableString *phoneContactStr = [[NSMutableString alloc] init];
    for (int i = 0; i < numberOfPeoples; i++) {
        ABRecordRef person = CFArrayGetValueAtIndex(peoples, i);
        NSString * xing =  (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString * ming =   (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"LXR3--%@",  (__bridge NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty));
        NSLog(@"LXR4--%@", (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty));
        NSLog(@"LXR5--%@",   (__bridge NSData*)ABPersonCopyImageData(person));
        //读取电话多值
        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
        NSString * phoneNo;
        for (int k = 0; k<ABMultiValueGetCount(phone); k++){
            //获取电话Label
            NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
            //获取該Label下的电话值
            NSString * tempstr = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
            NSArray *array = [NSArray arrayWithObjects:personPhoneLabel, tempstr, nil];
            phoneNo = array[1];
            NSLog(@"phone--%@",array[1]);
        }
        NSString * name;
        if (!xing) {
            name = ming;
        }else{
            name = [NSString stringWithFormat:@"%@%@",ming,xing];
        }
        NSMutableString * string = [NSMutableString stringWithFormat:@"{\"name\" : \"%@\",\"mobile\" : \"%@\"},",name,phoneNo];
        [phoneContactStr appendString:string];
        
        // [phoneArray addObject:dic];
        // NSLog(@"contactArr--%@",phoneArray);
    }
    if(numberOfPeoples>0){
        [self uploadPhoneContactWithString:phoneContactStr];
    }
}
-(void)uploadPhoneContactWithString:(NSMutableString*)string{
    NSString *  otherStr =  [string substringToIndex:string.length-1];
    string = [NSMutableString stringWithFormat:@"[%@]",otherStr];
    NSLog(@"电话---stirng----%@",string);
    
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"contact"];
    
    
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJDomain,@"log.php?"];
    NSDictionary * paraDic = @{@"action":@"uploadcontacts",
                               @"token":@"dev",
                               @"contacts":string,
                               @"username":@""};
    [THJNetWork postNetDataFromUrl:urlstr parameters:paraDic callback:^(id receiveData, NSError *error){
        NSLog(@"result---%@",receiveData);
    }];
}
@end
